const {
  $util,
} = getApp();
const app = getApp();

Page({
  data: {
    list: [],
    bannerList: [],
    isSearch: true,
    cateid: '',
    page: 1,
    hasMore: true,
    loaded: false,
    abnorData: {
      imgSrc: '/common/static/images/abnor-16.png',
      text: '暂无相关资讯',
    },
    nav: [],
    keyword: '',
    secId: '', // 资讯二级分类 id
    useTabbar: !(app.tabbarindex === 0 && app.siteInfo.tabbar_type === '1'),
  },
  onLoad(opts) {
    this.setData({
      cateid: opts.id || '',
    });
    wx.setNavigationBarTitle({
      title: opts.barTitle && decodeURIComponent(opts.barTitle) || '资讯',
    });
  },
  onShow() {
    this.setData({
      page: 1,
    }, () => {
      this.getNavList();
    });
  },
  onReachBottom() {
    this.data.hasMore && this.getList();
  },
  changeNav(e) {
    let {
      id,
      subcate = [],
    } = e.detail.obj;
    this.setData({
      page: 1,
      cateid: id,
      secId: id,
      keyword: '', // 清空搜索关键词
      newSecCate: subcate,
      isSecCate: false,
    }, () => {
      this.getList();
      this.getBannerList();
    });
  },
  changeSecNav(e) {
    let {
      id,
      pid,
    } = e.currentTarget.dataset;
    this.setData({
      page: 1,
      cateid: pid,
      secId: id,
      isSecCate: true,
      keyword: '', // 清空搜索关键词
    }, () => {
      this.getList();
    });
  },
  getNavList() {
    let {
      cateid,
      secId,
      isSecCate = false,
    } = this.data;
    $util.request({
      url: 'entry/wxapp/information.category.index',
      data: {},
      showLoading: false,
      success: (res) => {
        let nav = res.data;
        nav = [...[{
          id: '',
          title: '全部',
          subcate: [],
        }], ...nav];
        let idx = nav.findIndex(item => Number(cateid) === Number(item.id));
        if (idx === -1) {
          for (let i = 0; i < nav.length; i++) {
            if (nav[i].subcate.findIndex(item => Number(cateid) === Number(item.id)) !== -1) {
              isSecCate = true;
              idx = i;
              secId = cateid;
              cateid = nav[i].id;
              break;
            }
          }
        }
        this.setData({
          nav,
          defIdx: idx,
          isSecCate,
          cateid,
          secId,
          loaded: true,
          newSecCate: idx !== -1 ? nav[idx].subcate : [],
        }, () => {
          this.getList();
          this.getBannerList();
        });
      },
    });
  },
  /**
   * 获取轮播图
   */
  getBannerList() {
    let {
      cateid,
    } = this.data;
    $util.request({
      url: 'entry/wxapp/system.slide.index',
      data: {
        type: 1,
        cateid: cateid,
      },
      showLoading: false,
      success: (res) => {
        this.setData({
          bannerList: res.data,
        });
      },
    });
  },
  getList() {
    const {
      page,
      cateid,
      list: d_list,
      keyword,
      isSecCate,
      secId,
    } = this.data;
    let param = {
      page,
    };
    param.cateid = isSecCate ? secId : cateid;
    if (keyword !== '') {
      param.keyword = keyword;
    }
    $util.request({
      url: 'entry/wxapp/information.news.get_list',
      data: param,
      success: (res) => {
        let list = res.data;
        if (page > 1) {
          list = [...d_list, ...list];
        }
        this.setData({
          list,
          hasMore: res.data.length !== 0,
          loaded: true,
          page: res.data.length > 0 ? page + 1 : page,
          newsset: res.newsset,
        });
      },
    });
  },
  // 搜索
  getSearch(e) {
    this.setData({
      page: 1,
      keyword: e.detail.keyword,
    });
    this.getList();
  },
  bannerJump(e) {
    let {
      ztype,
      path = '',
      appid = '',
      title,
    } = e.currentTarget.dataset.item;
    if (Number(ztype) === 1) {
      let urlArr = path.split('?');
      let paramArr = {};
      paramArr.barTitle = title;
      if (urlArr.length > 1) {
        let param = urlArr[1].split('&');
        for (let i = 0; i < param.length; i++) {
          paramArr[param[i].split('=')[0]] = param[i].split('=')[1];
        }
      }
      app.util.navigateTo(urlArr[0], paramArr);
    } else if (Number(ztype) === 2) {
      wx.navigateToMiniProgram({
        appId: appid,
        path,
        envVersion: 'release',
      });
    } else if (Number(ztype) === 3) {
      const url = `/ws_party_const/pages/web/web?webview_url=${path}`;
      wx.navigateTo({
        url,
      });
    }
  },
  onShareAppMessage(e) {
    return {
      path: '/ws_party_const/pages/news-list/news-list',
    };
  },
});